package com.leno.mmall.controller.backend;

import com.github.pagehelper.PageInfo;
import com.leno.mmall.common.Const;
import com.leno.mmall.common.ResponseCode;
import com.leno.mmall.common.ServerResponse;
import com.leno.mmall.pojo.Order;
import com.leno.mmall.pojo.PayInfo;
import com.leno.mmall.pojo.Product;
import com.leno.mmall.pojo.User;
import com.leno.mmall.service.IFileService;
import com.leno.mmall.service.IOrderService;
import com.leno.mmall.service.IProductService;
import com.leno.mmall.service.IUserService;
import com.leno.mmall.vo.OrderVo;
import com.leno.mmall.vo.ProductDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/manage/order/")
public class OrderManageController {
    //    Autowired  Resource
    @Autowired
    public IOrderService iOrderService;

    @Autowired
    public IUserService iUserService;

    @Autowired
    public IFileService iFileService;


//####1.订单List
    @RequestMapping(value = "list.do")
    public ServerResponse<PageInfo> list(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            HttpSession session
    ) {
        User user = (User) session.getAttribute(Const.CURRENT_Admin);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录，请先登录");
        }
        if (iUserService.checkAdminRole(user).isSuccess()) {
            return iOrderService.getOrderList(user.getId(),pageNum,pageSize);
        } else {
            return ServerResponse.createByErrorMessage("无权限操作");
        }
    }

//####2.按订单号查询
@RequestMapping("search.do")
public ServerResponse<PageInfo> search(HttpSession session, Long orderNo,
                                       @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){

    User user = (User) session.getAttribute(Const.CURRENT_Admin);
    if (user == null) {
        return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录，请先登录");
    }
    if (iUserService.checkAdminRole(user).isSuccess()) {
        return iOrderService.manageSearch(orderNo,pageNum,pageSize);
    } else {
        return ServerResponse.createByErrorMessage("无权限操作");
    }
}

@RequestMapping("detail.do")
public ServerResponse<OrderVo> detail(Long orderNo, HttpSession session) {

    User user = (User) session.getAttribute(Const.CURRENT_Admin);
    if (user == null) {
        return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录，请先登录");
    }
    if (iUserService.checkAdminRole(user).isSuccess()) {
        return iOrderService.getOrderDetail(user.getId(),orderNo);
    } else {
        return ServerResponse.createByErrorMessage("无权限操作");
    }
}

    @RequestMapping("send_goods.do")
    public ServerResponse<String> send_goods(Long orderNo, HttpSession session) {

        User user = (User) session.getAttribute(Const.CURRENT_Admin);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录，请先登录");
        }
        if (iUserService.checkAdminRole(user).isSuccess()) {
            return iOrderService.manageSendGoods(orderNo);
        } else {
            return ServerResponse.createByErrorMessage("无权限操作");
        }
    }
}
